package Admin;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JRadioButton;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;

import java.awt.Checkbox;

import javax.swing.UIManager;
import javax.swing.JCheckBox;
import javax.swing.ButtonGroup;
import javax.swing.JToggleButton;

import java.awt.Color;

import javax.swing.JTextArea;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class reservation extends JPanel {
	JPanel contentPane;
	private JTextField textField;
	private JComboBox comboBox;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel lblEmail;
	private JLabel label_8;
	private JLabel photo;
	private JButton confirm;
	private JLabel rno;
	private JLabel mid;
	private JLabel irum;
	private JLabel dmid;
	private JLabel price;
	private JLabel date;
	private JLabel time;
	private JScrollPane scrollPane_1;
	private JLabel tel;
	private JLabel mail;
	private JLabel label_7;
	private JLabel label_9;
	private JLabel item;

	String[] category = new String[] { "전체", "디자이너", "예약일" };
	String selsctcategory = "all";
	private JRadioButton approve;
	private JRadioButton rejection;
	private JRadioButton afterdate;
	private JRadioButton befordate;
	private JRadioButton alldate;
	private JRadioButton allc;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	private JLabel lblNewLabel_4;
	private JLabel label_10;
	private JLabel pic;
	private JButton btnNewButton_2;
	private JCheckBox chckbxNewCheckBox;
	private JTextArea comment;

	ManageDTO dto;
	private JButton btnNewButton_1;
	ReservationVo v;
	/**
	 * Create the panel.
	 */
	public reservation(JPanel contentPane) {
		setBackground(Color.WHITE);
		this.contentPane = contentPane;
		setLayout(null);
		add(getTextField());
		add(getComboBox());
		add(getBtnNewButton());
		add(getScrollPane());
		add(getLblNewLabel());
		add(getLblNewLabel_1());
		add(getLabel());
		add(getLabel_1());
		add(getLabel_2());
		add(getLabel_3());
		add(getLabel_4());
		add(getLabel_5());
		add(getLabel_6());
		add(getLblEmail());
		add(getLabel_8());
		add(getLabel_7_1());
		add(getConfirm());
		add(getLabel_7_3());
		add(getLabel_7_4());
		add(getLabel_7_5());
		add(getLabel_7_6());
		add(getLabel_7_7());
		add(getLabel_7_8());
		add(getLabel_7_9());
		add(getScrollPane_1());
		add(getLabel_7_11());
		add(getLabel_7_12());
		add(getLabel_7_13());
		add(getLabel_9());
		add(getItem());
		add(getApprove());
		add(getRejection());
		add(getAfterdate());
		add(getBefordate());
		add(getAlldate());
		add(getAllc());
		add(getLblNewLabel_4());
		add(getLabel_10());
		add(getPic());
		add(getBtnNewButton_2());
		add(getCheckBox_1());
		add(getBtnNewButton_1());

	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(137, 92, 196, 21);
			textField.setColumns(10);
		}
		return textField;
	}

	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setBackground(Color.WHITE);
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					switch (comboBox.getSelectedIndex()) {
					case 0:
						selsctcategory = "all";
						break;
					case 1:
						selsctcategory = "dmid";
						break;
					case 2:
						selsctcategory = "days";
						break;
					}

				}
			});
			comboBox.setFont(new Font("Gulim", Font.PLAIN, 12));
			comboBox.setModel(new DefaultComboBoxModel(category));
			comboBox.setBounds(28, 92, 97, 21);
		}
		return comboBox;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					select();

				}
			});
			btnNewButton.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton.setBounds(345, 91, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(28, 124, 494, 397);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					dto = new ManageDTO();
					v = new ReservationVo();
					v = dto.selectONEreservation((Integer) table.getValueAt(
							table.getSelectedRow(), 0));

					rno.setText(v.getBno() + "");
					mid.setText(v.getMid());
					irum.setText(v.getC_irum());
					dmid.setText(v.getD_irum());
					String[] temp = v.getItems().split(",");
					ManageDTO dto2 = new ManageDTO();
					ArrayList<String> al = dto2.hairname(temp);
					item.setText(al.toString());
					price.setText(v.getCost() + "원");
					date.setText(v.getDays());
					time.setText(v.getClock() + "시");
					comment.setText(v.getComment());
					tel.setText(v.getTel());
					mail.setText(v.getMail());
					
					label_7.setText(v.getRespose());
					if(v.getConfirm().equals("false")){
						chckbxNewCheckBox.setSelected(false);
					}else if(v.getConfirm().equals("true")){
						chckbxNewCheckBox.setSelected(true);
					}
					
					if(v.getAttfile()!=null){
						pic.setText(v.getAttfile());
						btnNewButton_2.setEnabled(true);
					}else{
						pic.setText("");
						btnNewButton_2.setEnabled(false);
					}
					
				}
			});
		}
		return table;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("예약 번호");
			lblNewLabel.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel.setBounds(563, 125, 62, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ID");
			lblNewLabel_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel_1.setBounds(552, 157, 24, 15);
		}
		return lblNewLabel_1;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("고객 명");
			label.setFont(new Font("Gulim", Font.PLAIN, 12));
			label.setBounds(674, 157, 46, 15);
		}
		return label;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("디자이너명");
			label_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_1.setBounds(552, 182, 84, 15);
		}
		return label_1;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("합산 가격");
			label_2.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_2.setBounds(552, 235, 57, 15);
		}
		return label_2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("날짜");
			label_3.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_3.setBounds(552, 260, 32, 15);
		}
		return label_3;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("시간");
			label_4.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_4.setBounds(682, 260, 38, 15);
		}
		return label_4;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("코멘트");
			label_5.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_5.setBounds(552, 285, 46, 15);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("연락처");
			label_6.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_6.setBounds(552, 367, 46, 15);
		}
		return label_6;
	}

	private JLabel getLblEmail() {
		if (lblEmail == null) {
			lblEmail = new JLabel("E-mail");
			lblEmail.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblEmail.setBounds(552, 392, 46, 15);
		}
		return lblEmail;
	}

	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("수신동의");
			label_8.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_8.setBounds(552, 417, 57, 15);
		}
		return label_8;
	}

	private JLabel getLabel_7_1() {
		if (photo == null) {
			photo = new JLabel("사진");
			photo.setFont(new Font("Gulim", Font.PLAIN, 12));
			photo.setBounds(552, 442, 38, 15);
		}
		return photo;
	}

	private JButton getConfirm() {
		if (confirm == null) {
			confirm = new JButton("확인");
			confirm.setBackground(Color.WHITE);
			confirm.setFont(new Font("Gulim", Font.PLAIN, 12));
			confirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					dto = new ManageDTO();

					if (chckbxNewCheckBox.isSelected()) {

						if (dto.reservationConfirm(Integer.parseInt(rno
								.getText()),"true")) {
							JOptionPane.showMessageDialog(reservation.this,
									"승인 처리 완료");
							select();
						} else {
							JOptionPane.showMessageDialog(reservation.this,
									"승인 처리 오류");
						}
					}else{
						if (dto.reservationConfirm(Integer.parseInt(rno
								.getText()),"false")) {
							JOptionPane.showMessageDialog(reservation.this,
									"거부 처리 완료");
							select();
						} else {
							JOptionPane.showMessageDialog(reservation.this,
									"거부 처리 오류");
						}
					}

				}
			});
			confirm.setBounds(552, 491, 97, 23);
		}
		return confirm;
	}

	private JLabel getLabel_7_3() {
		if (rno == null) {
			rno = new JLabel("");
			rno.setBounds(626, 125, 84, 15);
		}
		return rno;
	}

	private JLabel getLabel_7_4() {
		if (mid == null) {
			mid = new JLabel("");
			mid.setBounds(588, 157, 57, 15);
		}
		return mid;
	}

	private JLabel getLabel_7_5() {
		if (irum == null) {
			irum = new JLabel("");
			irum.setBounds(732, 157, 57, 15);
		}
		return irum;
	}

	private JLabel getLabel_7_6() {
		if (dmid == null) {
			dmid = new JLabel("");
			dmid.setBounds(634, 182, 57, 15);
		}
		return dmid;
	}

	private JLabel getLabel_7_7() {
		if (price == null) {
			price = new JLabel("");
			price.setBounds(626, 235, 94, 15);
		}
		return price;
	}

	private JLabel getLabel_7_8() {
		if (date == null) {
			date = new JLabel("");
			date.setBounds(588, 260, 90, 15);
		}
		return date;
	}

	private JLabel getLabel_7_9() {
		if (time == null) {
			time = new JLabel("");
			time.setBounds(732, 260, 57, 15);
		}
		return time;
	}

	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(551, 304, 246, 53);
			scrollPane_1.setViewportView(getComment());
		}
		return scrollPane_1;
	}

	private JLabel getLabel_7_11() {
		if (tel == null) {
			tel = new JLabel("");
			tel.setBounds(610, 367, 139, 15);
		}
		return tel;
	}

	private JLabel getLabel_7_12() {
		if (mail == null) {
			mail = new JLabel("");
			mail.setBounds(610, 392, 139, 15);
		}
		return mail;
	}

	private JLabel getLabel_7_13() {
		if (label_7 == null) {
			label_7 = new JLabel("");
			label_7.setBounds(621, 417, 57, 15);
		}
		return label_7;
	}

	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("시술 항목");
			label_9.setFont(new Font("굴림", Font.PLAIN, 12));
			label_9.setBounds(552, 210, 57, 15);
		}
		return label_9;
	}

	private JLabel getItem() {
		if (item == null) {
			item = new JLabel("");
			item.setBounds(610, 210, 203, 15);
		}
		return item;
	}

	private JRadioButton getApprove() {
		if (approve == null) {
			approve = new JRadioButton("승인만 보기");
			approve.setBackground(Color.WHITE);
			approve.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup_1.add(approve);
			approve.setBounds(196, 63, 97, 23);
		}
		return approve;
	}

	private JRadioButton getRejection() {
		if (rejection == null) {
			rejection = new JRadioButton("미 승인만 보기");
			rejection.setBackground(Color.WHITE);
			rejection.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup_1.add(rejection);
			rejection.setBounds(307, 63, 121, 23);
		}
		return rejection;
	}

	private JRadioButton getAfterdate() {
		if (afterdate == null) {
			afterdate = new JRadioButton("지난 예약만 보기");
			afterdate.setBackground(Color.WHITE);
			afterdate.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup.add(afterdate);
			afterdate.setBounds(307, 35, 121, 23);
		}
		return afterdate;
	}

	private JRadioButton getBefordate() {
		if (befordate == null) {
			befordate = new JRadioButton("지난 예약 제외 ");
			befordate.setBackground(Color.WHITE);
			befordate.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup.add(befordate);
			befordate.setBounds(196, 35, 114, 23);
		}
		return befordate;
	}

	private JRadioButton getAlldate() {
		if (alldate == null) {
			alldate = new JRadioButton("전체보기");
			alldate.setBackground(Color.WHITE);
			alldate.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup.add(alldate);
			alldate.setSelected(true);
			alldate.setBounds(117, 35, 75, 23);
		}
		return alldate;
	}

	private JRadioButton getAllc() {
		if (allc == null) {
			allc = new JRadioButton("전체보기");
			allc.setBackground(Color.WHITE);
			allc.setFont(new Font("Gulim", Font.PLAIN, 12));
			buttonGroup_1.add(allc);
			allc.setSelected(true);
			allc.setBounds(117, 63, 75, 23);
		}
		return allc;
	}

	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("날짜 조건  :");
			lblNewLabel_4.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel_4.setBounds(32, 39, 82, 15);
		}
		return lblNewLabel_4;
	}

	private JLabel getLabel_10() {
		if (label_10 == null) {
			label_10 = new JLabel("승인 조건  :");
			label_10.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_10.setBounds(32, 67, 82, 15);
		}
		return label_10;
	}

	private JLabel getPic() {
		if (pic == null) {
			pic = new JLabel("");
			pic.setBounds(588, 443, 97, 15);
		}
		return pic;
	}

	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("보기");
			btnNewButton_2.setEnabled(false);
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ViewPhoto vv = new ViewPhoto(v.getAttfile());
					vv.setVisible(true);
				}
			});
			btnNewButton_2.setBounds(690, 439, 73, 23);
		}
		return btnNewButton_2;
	}

	private JCheckBox getCheckBox_1() {
		if (chckbxNewCheckBox == null) {
			chckbxNewCheckBox = new JCheckBox("예약 승인");
			chckbxNewCheckBox.setBackground(Color.WHITE);
			chckbxNewCheckBox.setFont(new Font("굴림", Font.BOLD, 15));
			chckbxNewCheckBox.setForeground(Color.RED);
			chckbxNewCheckBox.setBounds(552, 463, 108, 22);
		}
		return chckbxNewCheckBox;
	}

	private JTextArea getComment() {
		if (comment == null) {
			comment = new JTextArea();
		}
		return comment;
	}

	public void select() {

		Vector<String> vec = new Vector<String>();
		vec.add("예약번호");
		// vec.add("ID");
		vec.add("고객명");
		// vec.add("디자이너ID");
		vec.add("디자이너 명");
		vec.add("시술 항목");
		// vec.add("시술 가격");
		vec.add("예약일");
		vec.add("예약 시간");
		// vec.add("코멘트");
		// vec.add("연락처");
		// vec.add("Email");
		// vec.add("사진");
		// vec.add("수신동의");
		vec.add("예약 상태");

		String[] condition = new String[2];
		if (alldate.isSelected()) {
			condition[0] = "all";
		} else if (afterdate.isSelected()) {
			condition[0] = "after";
		} else if (befordate.isSelected()) {
			condition[0] = "befor";
		}

		if (allc.isSelected()) {
			condition[1] = "all";
		} else if (rejection.isSelected()) {
			condition[1] = "rejection";
		} else if (approve.isSelected()) {
			condition[1] = "approve";
		}

		DefaultTableModel tmodel = new DefaultTableModel(vec, 0);
		dto = new ManageDTO();

		ArrayList<ReservationVo> al = new ArrayList<ReservationVo>();
		al = dto.selectreservation(selsctcategory, textField.getText(),
				condition);

		for (ReservationVo vo : al) {
			Vector v = new Vector();

			v.add(vo.getBno());
			v.add(vo.getC_irum());
			v.add(vo.getD_irum());
			v.add(vo.getItems());
			v.add(vo.getDays());
			v.add(vo.getClock());
			v.add(vo.getConfirm());

			tmodel.addRow(v);

		}

		table.setModel(tmodel);
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("삭제");
			btnNewButton_1.setBackground(Color.WHITE);
			btnNewButton_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					dto = new ManageDTO();
					
					if(!rno.getText().equals("")){
					
					if(dto.deletereservation(Integer.parseInt(rno.getText()))){
						JOptionPane.showMessageDialog(reservation.this, "예약 삭제 완료");
						select();
					}else{
						JOptionPane.showMessageDialog(reservation.this, "예약 삭제 오류");
					}
					}else{
						JOptionPane.showMessageDialog(reservation.this, "삭제할 예약 정보를 선택 하세요");
					}
				}
			});
			btnNewButton_1.setBounds(692, 491, 97, 23);
		}
		return btnNewButton_1;
	}
}
